package com.hellenic.mod;

import static com.hellenic.mod.ValideForm.valideEMail;
import static com.hellenic.mod.ValideForm.valideString;
import static com.hellenic.mod.ValideForm.valideTel;


import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.hibernate.SessionFactory;

import com.hellenic.beans.ReservationBean;
import com.hellenic.dao.ReservationDao;


public class ReservationMod {
    private static final String MESSAGE_SUCCESS = "Opération réussite";
    private static final String MESSAGE_ERROR   = "L'opération a échouée";
    private static final String CHAMP_NOM       = "nom";
    private static final String CHAMP_PRENOM    = "prenom";
    private static final String CHAMP_EMAIL     = "email";
    private static final String CHAMP_TELEPHONE = "tel";
    private static final String CHAMP_PAYS      = "pays";
    private static final String CHMAP_PROPRIETE = "idPropriete";

    private ReservationDao      reservationDao;

    public ReservationMod( SessionFactory sf ) {
        // intanciation d'un objet DAO
        this.reservationDao = new ReservationDao( sf );
        System.out.println( "Message : Instanciation d'un objet Reservation (METIER)" );
    }

    public HashMap<String, String> createResa( HttpServletRequest request ) {
        // recupération des GET
        String nom = request.getParameter( CHAMP_NOM );
        String prenom = request.getParameter( CHAMP_PRENOM );
        String email = request.getParameter( CHAMP_EMAIL );
        String telephone = request.getParameter( CHAMP_TELEPHONE );
        String pays = request.getParameter( CHAMP_PAYS );
        String propriete = request.getParameter( CHMAP_PROPRIETE );
        String date = ""; //TODO A modifier

        HashMap<String, String> result = new HashMap<String, String>();

        // validation du champ nom
        try {
            valideString( nom, "nom" );
        } catch ( Exception e ) {
            result.put( CHAMP_NOM, e.getMessage() );
        }

        // validation du champ prenom
        try {
            valideString( prenom, "prenom" );
        } catch ( Exception e ) {
            result.put( CHAMP_PRENOM, e.getMessage() );
        }

        // validation du champ email
        try {
            valideEMail( email );
        } catch ( Exception e ) {
            result.put( CHAMP_EMAIL, e.getMessage() );
        }

        // validation du champ tel
        try {
            valideTel( telephone );
        } catch ( Exception e ) {
            result.put( CHAMP_TELEPHONE, e.getMessage() );
        }

        if ( result.isEmpty() ) {
            boolean erreur = false;
            erreur = reservationDao.create( nom, prenom, email, telephone, date, pays, propriete );
            if ( erreur ) {
                result.put( "globalResult", MESSAGE_SUCCESS );
                result.put( "globalTypeResult", MESSAGE_ERROR);
            }
        } else {
            result.put( "globalResult", "Votre demande à bien etait enregistrer" );
            result.put( "globalTypeResult", "alert-success" );
        }

        result.put( "idPropriete", CHMAP_PROPRIETE );
        return result;
    }
    
    public ArrayList<ReservationBean> listingResa(HttpServletRequest request ){
        ArrayList<ReservationBean> result = null;
        result = ( ArrayList<ReservationBean> ) reservationDao.getAll();
        return result;
    }
}